from django.shortcuts import render, redirect
from webapp.models import Department


# Create your views here.
def depart_list(request):
    data = Department.objects.all()
    return render(request, "depart_list.html", {"data": data})


def depart_add(request):
    if request.method == "POST":
        data = request.POST.get("title")
        Department.objects.create(title=data)
        return redirect("/depart/list")
    return render(request, "depart_add.html")


def depart_delete(request):
    r_id = request.GET.get("id")
    Department.objects.filter(id=r_id).delete()
    return redirect("/depart/list")


def depart_update(request):
    if request.method == "POST":
        r_id = request.POST.get("id")
        r_title = request.POST.get("title")
        Department.objects.filter(id=r_id).update(title=r_title)
        return redirect("/depart/list")
    r_id = request.GET.get("id")
    data = Department.objects.filter(id=r_id).first()
    return render(request, "depart_update.html", {"data": data})
